﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;

namespace YDWin.Customer
{
    public partial class AddCustomerForm : AddForm
    {
        CustomerService svr = new CustomerService();
        FactoryService fs = new FactoryService();
        public T_Customers editTu = new T_Customers();
        public string state = "";
        public AddCustomerForm()
        {
            InitializeComponent();
 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
       
            T_Customers fact = new T_Customers();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            if (txtCustomerName.Text.Trim().Equals(""))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "客户姓名不能为空!\n"); 
                return;
            } 
            
            fact.CustomerName = txtCustomerName.Text;
            fact.Phone = txtPhone.Text;
            fact.Address = txtAddress.Text;
            fact.Memo = txtMemo.Text;

            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                svr.EditItem(fact);
            }
            else
            {
                svr.AddItem(fact);
            }
      

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "客户信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddCustomerForm_Load(object sender, EventArgs e)
        {
        
            if (!state.Equals("") && state.Equals("Edit")) 
            { 
                txtCustomerName.Text = editTu.CustomerName;
                txtPhone.Text= editTu.Phone;
                txtAddress.Text = editTu.Address;
                txtMemo.Text = editTu.Memo;
            }
        }
 
    }
}